--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  file:    factoryOrders.lua
--  brief:   Handles what the Factories are producing, and the initial orders they give the units they produce
--  author:  Sean Heron
--
--  Copyright (C) 2009.
--  Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function gadget:GetInfo()
  return {
    name      = "factoryOrders",
    desc      = "Give orders to non controlled Factories",   -- Ah could this not be a widget... hmm if widgets were locked, I guess yes - how easy to hack ? Might work nicer that way (problem perhaps difficulties with interaction with Gadgets - Hero abilities...
    author    = "Sean Heron",
    date      = "September 3rd, 2009",
    license   = "GNU GPL, v2 or later",
    layer     = 1,
    enabled   = true
  }
end




--local myteam = Spring.GetMyTeamID()      -- their is no myteam is there.. in Gadgets ? ve shall see.    I think I was right...

local landfactory = UnitDefNames.landfactory.id
local hunchback   = UnitDefNames.hunchback.id


if (gadgetHandler:IsSyncedCode()) then

--  SYNCED                                       -- might need to go in unsynced actually...
--------------------------------------------------------------------------------


function gadget:Update()
		 -- for later I guess
end

function gadget:GameFrame(f)
	if (f == 1) then
		Spring.Echo("Produce Units! Storm the Enemy!")
		
		local allmyfactories = {}              -- just me for now
		allmyfactories = Spring.GetTeamUnitsByDefs(1,landfactory)               --started complicated... lets see if all goes well...ah, but if it's not selectable...? maybe need to use gadget after all (and perhaps even that won't do..
		for i,unitID in pairs(allmyfactories) do                                          -- wow, that was painless - problem was my second suspision - user not allowed to order, so neither is widget!
		    Spring.Echo("working?!")       --there's this strange mystery unit cropping up... unitID 2 ? giving an error as well
		    Spring.Echo(unitID)
		    Spring.GiveOrderToUnit(unitID, -hunchback, {}, {})
		    Spring.GiveOrderToUnit(unitID, -hunchback, {}, {})
         	Spring.GiveOrderToUnit(unitID, CMD.REPEAT, { 1 }, {})
		end
	end
end

----------------------------------------------------------------------------------
---- UNSYNCED               -- do I care ? am I busting something, would it work in Unsynced but I'm doing it wrong ?
----------------------------------------------------------------------------------



end